<template>
<el-dialog :title="$t('supplier.edit_service')" v-model="dialogVisible" @close="dialogFormVisible" :close-on-click-modal="false"
               :close-on-press-escape="false">
        <el-form size="small" :model="form" :rules="formRules" ref="form">
            <el-form-item :label="$t('supplier.name')" :label-width="formLabelWidth" prop="name">
                <el-input v-model="form.name" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item :label="$t('supplier.description')" :label-width="formLabelWidth" prop="description">
              <el-input v-model="form.description" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item :label="$t('supplier.image')" prop="logo" :label-width="formLabelWidth">
              <el-row>
                <el-button type="primary" @click="openUpload">{{$t('supplier.choose_image')}}</el-button>
                <div v-if="form.logo != ''" class="img">
                  <img :src="form.logo" width="100" height="100" />
                </div>
              </el-row>
              <div class="gray9">{{$t('supplier.image_size_tip_42')}}</div>
            </el-form-item>
            <el-form-item :label="$t('supplier.status')" :label-width="formLabelWidth">
              <el-radio-group v-model="form.status">
                <el-radio :label="1">{{$t('supplier.show')}}</el-radio>
                <el-radio :label="0">{{$t('supplier.hide')}}</el-radio>
              </el-radio-group>
            </el-form-item>
        </el-form>
        <template #footer>
        <div class="dialog-footer">
            <el-button @click="dialogFormVisible">{{$t('supplier.cancel0')}}</el-button>
            <el-button type="primary" @click="editCategory" :loading="loading">{{$t('supplier.confirm0')}}</el-button>
        </div>
        </template>
        <!--上传图片组件-->
        <Upload v-if="isupload" :isupload="isupload" :type="type" @returnImgs="returnImgsFunc">{{$t('supplier.upload_image')}}</Upload>
    </el-dialog>
</template>

<script>
import SupplierApi from '@/api/supplier.js';
import Upload from '@/components/file/Upload.vue';
export default {
  components: {
    Upload
  },
  data() {
    return {
      form: {
        serviceSecurityId: 0,
        name: '',
        logo: '',
        description: '',
        status: 1
      },
      formRules: {
        name: [{
          required: true,
          message: $t("supplier.input_name"),
          trigger: 'blur'
        }],
        description: [{
          required: true,
          message: $t("supplier.input_description"),
          trigger: 'blur'
        }],
        logo: [{
          required: true,
          message: $t("supplier.upload_image_required"),
          trigger: 'blur'
        }]
      },
      /*左边长度*/
      formLabelWidth: '120px',
      /*是否显示*/
      dialogVisible: false,
      loading: false,
      /*是否上传图片*/
      isupload: false
    };
  },
  props: ['open_edit', 'editform'],
  created() {
    this.dialogVisible = this.open_edit;
    this.form.serviceSecurityId = this.editform.serviceSecurityId;
    this.form.name = this.editform.name;
    this.form.description = this.editform.description;
    this.form.status = this.editform.status;
    this.form.logo = this.editform.logo;
  },
  methods: {
    /*修改*/
    editCategory() {
      let self = this;
      let params = this.form;
      self.$refs.form.validate(valid => {
        if (valid) {
          self.loading = true;
          SupplierApi.editSecurity(params, true).then(data => {
            self.loading = false;
            ElMessage({
              message: $t("supplier.edit_success_message"),
              type: 'success'
            });
            self.dialogFormVisible(true);
          }).catch(error => {
            self.loading = false;
          });
        }
      });
    },
    /*关闭弹窗*/
    dialogFormVisible(e) {
      if (e) {
        this.$emit('closeDialog', {
          type: 'success',
          openDialog: false
        });
      } else {
        this.$emit('closeDialog', {
          type: 'error',
          openDialog: false
        });
      }
    },
    /*上传*/
    openUpload(e) {
      this.type = e;
      this.isupload = true;
    },
    /*获取图片*/
    returnImgsFunc(e) {
      if (e != null && e.length > 0) {
        this.form.logo = e[0].fileath;
      }
      this.isupload = false;
    }
  }
};
</script>

